﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using CL.Business.Entity;
using CL.Dapper.Repository;
using CL.Enum.Common;
using Dapper;
using CL.View.Entity.Business;

namespace CL.Business.DAL
{
    public class SettleDayDAL : DataRepositoryBase<SettleDayEntity>
    {
        public SettleDayDAL(DbConnectionEnum conenum, IDbConnection Db = null) : base(conenum, Db)
        {

        }

        /// <summary>
        /// 展示预计分成数据
        /// </summary>
        /// <param name="AccountID">代理账号标识</param>
        /// <param name="currentTiem">显示年月份</param>
        /// <returns></returns>
        public List<udv_SettleDay> GetSettleDayDal(int AccountID, string currentTiem)
        {
            var Parms = new DynamicParameters();
            Parms.Add("@AccountID", AccountID);
            Parms.Add("@currentTiem", currentTiem + "%");
            string sql = "  SELECT [SettleDayID], [Day], [SettleTime], [BuyAmount], [RebateNum] FROM [dbo].[UN_SettleDay] WHERE AccountID = @AccountID AND Day LIKE @currentTiem ORDER BY SettleTime DESC ";
            var Entitys = new DataRepositoryBase<udv_SettleDay>(DbConnectionEnum.CaileBusiness).QueryList(sql, Parms).ToList();
            return Entitys;
        }

        /// <summary>
        /// 根据平台会员ID获取代理平台预计分成数据
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageLenght"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public List<udv_SettleDay> GetSettleDayForUserIDDal(long userID, int pageIndex, int pageLenght, ref int pageCount)
        {
            var Parms = new DynamicParameters();
            Parms.Add("@userID", userID, DbType.Int64);
            Parms.Add("@pageIndex", pageIndex, DbType.Int32);
            Parms.Add("@pageLength", pageLenght, DbType.Int32);
            Parms.Add("@pageCount", pageCount, DbType.Int32, ParameterDirection.Output);
            var listDay = new DataRepositoryBase<udv_SettleDay>(DbConnectionEnum.CaileBusiness).QueryList("udp_GetSettleDayForUserID", Parms, CommandType.StoredProcedure).ToList();
            pageCount = Parms.Get<int>("@pageCount");
            return listDay;
        }

        /// <summary>
        /// 计算预计分成
        /// </summary>
        public void CalSettlement()
        {
            new DataRepositoryBase<udv_SettleDay>(DbConnectionEnum.CaileBusiness).QueryList("udp_QuerySettlementNew");
        }

        /// <summary>
        /// 计算用户上个月的返点分成
        /// </summary>
        public void CalLastMonthSettlement()
        {
            new DataRepositoryBase<udv_SettleDay>(DbConnectionEnum.CaileBusiness).QueryList("udp_MonthSettlement");
        }

        /// <summary>
        /// 取消用户连续三个月推广未达标的推广资格, 以及结算上月之前推广未达标收益
        /// </summary>
        /// <returns></returns>
        public void GetUserNoEffectExtensionBal()
        {
                new DataRepositoryBase<udv_SettleDay>(DbConnectionEnum.CaileBusiness).QueryList("udp_CheckUserNoEffectExtension");
        }



        /// <summary>
        /// 批量插入对象
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public bool InsertSettleDay(List<SettleDayEntity> Entitys)
        {
            using (IDbTransaction tran = base.db.BeginTransaction())
            {
                try
                {
                    Entitys.ForEach((Entity) =>
                    {
                        base.Insert(Entity, tran);
                    });
                    tran.Commit();
                    return true;
                }
                catch
                {
                    tran.Rollback();
                    throw;
                }
            }
        }


        /// <summary>
        /// 查询用户充值
        /// </summary>
        /// <param name="GameName"></param>
        /// <returns></returns>
        public List<udv_SettleDay> QueryRecharge()
        {
            var Parms = new DynamicParameters();
            var Entitys = new DataRepositoryBase<udv_SettleDay>(DbConnectionEnum.CaileBusiness).QueryList("udp_AmountIncome", Parms, CommandType.StoredProcedure).ToList();
            return Entitys;
        }

        /// <summary>
        /// 查询用户充值退款
        /// </summary>
        /// <param name="GameName"></param>
        /// <returns></returns>
        public List<udv_SettleDay> QueryRefund()
        {
            var Parms = new DynamicParameters();
            var Entitys = new DataRepositoryBase<udv_SettleDay>(DbConnectionEnum.CaileBusiness).QueryList("udp_AmountRefund", Parms, CommandType.StoredProcedure).ToList();
            return Entitys;
        }


        /// <summary>
        /// 插入记录
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public int InsertEntity(SettleDayEntity Entity)
        {
            return base.Insert(Entity) ?? 0;
        }
    }
}
